package com.learn;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class Reflect_1 {
    public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchMethodException, InvocationTargetException, NoSuchFieldException {
        Class cat=Class.forName("com.learn.Cat");
        Object o=cat.newInstance();
        System.out.println(o.getClass());
      Method m=cat.getMethod("info");
      Method eat=cat.getMethod("eat");
       m.invoke(o);
       eat.invoke(o);
        Field field=cat.getField("name");
        System.out.println(field);
        System.out.println(o);
        Field color=cat.getField("color");
        System.out.println(color);
 color.set(o,"红色");
        System.out.println(o);
        System.out.println(color.get(o));
      //  System.out.println(color.get(color));
 //color.set(color,"卢瑟");
// System.out.println(color.get(color));
    }
}


class Cat{
    public String name="加菲猫" ;
    public int age=5;
    public String color="黄色";



    @Override
    public String toString() {
        return "Cat{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", color='" + color + '\'' +
                '}';
    }

    public void info(){
        System.out.println("hi  "+name);
    }

    public void eat(){
        System.out.println(name+"你吃饭了吗?");
    }
}
